package cn.getech.data.development.service.impl;

import cn.getech.data.development.entity.Alarm;
import cn.getech.data.development.entity.BdpAlarmLog;
import cn.getech.data.development.mapper.BdpAlarmLogMapper;
import cn.getech.data.development.model.dto.BdpAlarmLogSearchDto;
import cn.getech.data.development.model.vo.BdpAlarmLogVO;
import cn.getech.data.development.service.AlarmService;
import cn.getech.data.development.service.IBdpAlarmLogService;
import cn.getech.system.center.entity.SysUserEntity;
import cn.getech.system.center.service.SysUserService;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 告警-日志表 服务实现类
 * </p>
 *
 * @author cwp
 * @since 2020-12-01
 */
@Service
public class BdpAlarmLogServiceImpl extends ServiceImpl<BdpAlarmLogMapper, BdpAlarmLog> implements IBdpAlarmLogService {

    @Autowired
    private  BdpAlarmLogMapper  bdpAlarmLogMapper;

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private AlarmService alarmService;

    @Override
    public Page listByPage(BdpAlarmLogSearchDto searchDto) {
        Page<BdpAlarmLogVO> page=new Page<>(searchDto.getPage(),searchDto.getPageSize());
        try{
            searchDto.setTenantId(ShiroUtils.getTenantId());
            List<BdpAlarmLogVO> list= bdpAlarmLogMapper.selectAlarmLogList(page,searchDto);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * @Description 获取接收人列表
     * @Author  chengweiping
     * @Date   2020/12/8 14:33
     */
    @Override
    public Collection<SysUserEntity> getReceiverList(){
        List<Alarm> list1 = alarmService.list(new LambdaQueryWrapper<Alarm>().eq(Alarm::getTenantId, ShiroUtils.getTenantId()));
        List<Integer> ids = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list1)){
            ids = list1.stream().map(Alarm::getId).distinct().collect(Collectors.toList());
        }else{
            return new ArrayList<>();
        }
        Collection<BdpAlarmLog>  list=this.list(new LambdaQueryWrapper<BdpAlarmLog>().in(BdpAlarmLog::getAlarmId,ids));
        if(CollectionUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        List<Integer>  userIdList= list.stream().map(e->e.getReceivedUserId()).distinct().collect(Collectors.toList());
        if(CollectionUtil.isEmpty(userIdList)){
            return new ArrayList<>();
        }

        Collection userList= sysUserService.listByIds(userIdList);
        if(CollectionUtil.isEmpty(userList)){
            return new ArrayList<>();
        }
        return userList;
    }

    /**
     * @Description 获取创建人列表
     * @Author  chengweiping
     * @Date   2020/12/8 14:33
     */
    @Override
    public Collection<SysUserEntity> getCreaterList(){
        Collection<BdpAlarmLog>  list=this.list();
        if(CollectionUtil.isEmpty(list)){
            return new ArrayList<>();
        }
       List<Integer>  userIdList= list.stream().map(e->e.getReceivedUserId()).distinct().collect(Collectors.toList());

        if(CollectionUtil.isEmpty(userIdList)){
            return new ArrayList<>();
        }

        Collection userList= sysUserService.listByIds(userIdList);
        if(CollectionUtil.isEmpty(userList)){
            return new ArrayList<>();
        }
        return userList;
    }

}
